# -*- coding: utf-8 -*-
# @Time    : 2021/4/8 17:32
# @Author  : Chen
# @Email   : 2356458272@qq.com
# @File    : 三乐.py
# @Software: PyCharm
import MySQLdb

# 打开数据库连接
# db = MySQLdb.connect("localhost", "root", "root", "sanle", charset='utf8' )
#
# # 使用cursor()方法获取操作游标
# cursor = db.cursor()
goods = {'F00001':{'gname':'苹果','price':1.2,'kc':100},'F00002':{'gname':'香蕉','price':1.5,'kc':88},
         'F00003':{'gname':'栗子','price':3.5,'kc':90},'F00004':{'gname':'叶罗丽娃娃','price':8.5,'kc':9}}
ygsp = {'F00001':{'gname':'苹果','price':1.2,'account':1}}
# yh = {'root':'123456'}
ls = 0
#注册界面
def zhuce():
    global name
    db = MySQLdb.connect("localhost", "root", "root", "sanle", charset='utf8')
    cursor = db.cursor()
    print("注册界面".center(100, "*"))
    name = input("请输入用户名：")
    cursor.execute("SELECT username from userinfo")
    user = cursor.fetchall()
    if (name,) in user:
        print("用户名已存在")
    else:
        ps = input("请输入密码：")
        cursor.execute("insert into userinfo values('%s', '%s')" % (name, ps))
        db.commit()
        cursor.close()
        db.close()
        print("注册成功，请登录")


#登录界面
def denglu():
    # db = MySQLdb.connect("localhost", "root", "root", "sanle", charset='utf8')
    # cursor = db.cursor()
    print("登录界面".center(100, "*"))
    while 1:
        global username
        username = input("请输入用户名：")
        cursor.execute("SELECT username from userinfo ")
        user = cursor.fetchall()
        if (username,) in user:
            while 1:
                userpwd = input("请输入密码：")
                cursor.execute("SELECT passwd from userinfo where username =username ")
                passwd = cursor.fetchone()
                if userpwd in passwd[0]:
                    print("登录成功")
                    break
                else:
                    print("密码错误")
            break
        else:
            print("用户名不存在，请重新输入")

#商品展示
def goodlist():
    print("商品展示".center(100, "*"))
    cursor.execute("SELECT * from goodlist ")
    goodlist = cursor.fetchall()
    for i in range(len(goodlist)):
        print("编号:", goodlist[i][0], end=' ')
        print("商品名称：", goodlist[i][1], end=' ')
        print("商品价格：", goodlist[i][2], end=' ')
        print("库存：", goodlist[i][3])
    db.close()


#已购信息
def yigou():
    print("已购商品信息".center(100, "*"))
    ygsp = username+'ygsp'
    cursor.execute("SELECT * from %s" % (ygsp))
    yigou = cursor.fetchall()
    for i in range(len(yigou)):
        print("编号:", yigou[i][0], end=' ')
        print("商品名称：", yigou[i][1], end=' ')
        print("商品价格：", yigou[i][2], end=' ')
        print("数量：", yigou[i][3])
    db.close()
#商品购买
def pay():
    print("购买界面".center(100, "*"))
    a = input("请输入你要购买的商品编号：")
    cursor.execute("SELECT * from goodlist where id = a" )
    shop = cursor.fetchall()
    ygsp = username + 'ygsp'
    cursor.execute("insert into %s values(%s,'%s',%s,%s)" % (ygsp),(a,))
    # ygsp[a] = b
    global money,ls
    if int(money) > b['price']:
        if a in ygsp:
            c = ygsp[a]
            c['account'] = int(c['account']) + int(1)
            b['kc'] = int(b['kc']) - int(1)
        else:
            ls = ls + 1
            b['account'] = ls
            b['kc'] = int(b['kc']) - int(1)
            ygsp[a] = b
        ys = float(ys) - float(b['price'])
        print("购买成功")
        print("余额还有：", ys)
    else:
        print("余额不足")
#退出程序
def ex():
    a = input("请输入：")
    if a == 'exit':
        for bh, j in ygsp.items():
            print("编号:", bh, end=' ')
            print("商品名称：", j['gname'], end=' ')
            print("商品价格：", j['price'], end=' ')
            print("已购数量：", j['account'])
        print(ys)
        exit()
    else:
        pass

#主程序
db = MySQLdb.connect("localhost", "root", "root", "sanle", charset='utf8')
cursor = db.cursor()
print("输入1注册".center(50, "*"))
print("输入2登录".center(50, "*"))
while 1:
  c = 0
  b = input("请输入：")
  if int(b) == 1:
      zhuce()
      c = 1
  elif int(b) == 2:
     denglu()
     if c == 1:
        ys = int(input("请输入预算："))
        cursor.execute("insert into money values ('%s',%s)" %(name,ys))
        db.commit()
        cursor.close()
        db.close()
     cursor.execute("select money from money where username = username")
     money = cursor.fetchall()[0][0]
     print("你的预算为：",money)
     while 1:
            print("输入3查看商品".center(50,"*"))
            print("输入4进行购买".center(50,"*"))
            a = input("请选择：")
            if int(a) == 3:
                goodlist()
                ex()
            elif int(a) == 4:
                pay()
            elif int(a) == 5:
                yigou()
            else:
                break
  else:
      print("请重新选择")
